<template>
	<view class="login-page">
		<image class="background-img" src="@/static/2.png"></image>
		<image class="foreground-img" src="@/static/3.png"></image>
		<view class="login-form">
			<view class="title">欢迎登陆</view>
			<form class="form">
				<view class="input-group">
					<image src="@/static/sj.png"></image>
					<input type="text" name="accountName" placeholder="请输入车号" maxlength="11" v-model="forms.accountName" />
				</view>
				<view class="input-group">
					<image src="@/static/yz.png"></image>
					<input type="password" name="loginPass" maxlength="6" placeholder="请输入密码" v-model="forms.loginPass" />
					<!-- 	<view v-if="showText" class="send-code" @tap="getCode()">
					<view v-else class="send-code disabled">重新发送({{ second }})</view>  -->
				</view>
				<button type="primary" @tap="login">登录</button>
			</form>
		</view>

		<agree-vue @change="checkBoxChanage" />
	</view>
</template>

<script setup>
import { onLoad, onShow } from '@dcloudio/uni-app';
import { onBeforeMount, onMounted, reactive, ref, toRefs, getCurrentInstance } from 'vue';
import { storeToRefs } from 'pinia';
import http from '@/common/request.js';
import { urlToObj, sgoto, navTo, getImgSrc, setLoadingToast, setToast, setUserIdentity } from '@/common/utils.js';
import useStore from '@/store';
import agreeVue from '@/components/agree.vue';
import { getCode } from '../../../common/wx';

// 司机全局状态管理器
const { useLogisicsDriver, useWx } = useStore();

// state
const { userInfo } = storeToRefs(useLogisicsDriver);

const { getOpenId } = storeToRefs(useWx);

// action
const { companyLogin, getToken } = useLogisicsDriver;

// 登录表单
const forms = reactive({
	accountName: '', // CH911
	loginPass: '', // 200888
	remember: false
});

const checked = ref(false);

const checkBoxChanage = (e) => {
	// console.log('e ====> ', e);
	checked.value = e;
};
// 状态控制
const lockClick = (lock) => {
	forms.remember = lock;
};

// 登录
const login = () => {
	if (!checked.value) {
		setToast('请同意并勾选下方政策协议后操作~');
		return;
	}

	// 登陆
	try {
		forms.openid = getOpenId;
		companyLogin(forms);
	} catch (e) {
		//TODO handle the exception
		uni.hideLoading();
	}
};

onMounted(() => {
	setUserIdentity();
	if (getToken) {
		navTo('/pages/driver/index/index');
	} else {
		// 获取code
		getCode();
	}
});
</script>

<style scoped lang="scss">
@import './login.scss';
</style>
